﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Text;
using System.Globalization;

/// <summary>
/// HtmlOperation 的摘要说明
/// </summary>
public class HtmlOperation
{
    public HtmlOperation()
    {
        //
        // TODO: 在此处添加构造函数逻辑
        //
    }
    public static string ReplaceClass(DataTable tb,string oHtml,string expression)
    {
        string[] exp = expression.Split('|');
        int i = 0;
        int count = tb.Rows.Count;
        StringBuilder rHtml = new StringBuilder();
        try
        {
            foreach (string e in exp)
            {
                foreach (DataRow dr in tb.Rows)
                {
                    switch (e)
                    {
                        case "标题":
                            rHtml.Append("<li><a href=" + dr[i].ToString() + ">" + dr[i].ToString() + "</a></li>");
                            break;
                        case "时间":
                            rHtml.Append("<li>" + dr[i].ToString() + "</li>");
                            break;
                        case "分类":
                            rHtml.Append("<li><a href=" + dr[0].ToString() + "><h3>" + dr[1].ToString() +"</h3></a></li>");
                            break;
                        default:
                            break;
                    }
                }
                i++;
                oHtml = oHtml.Replace("{$" + e + "$}", rHtml.ToString());
                rHtml.Clear();
            }
            if (exp[0] != "分类") {
                for (int j = 0; j <= count; j++)
                {
                    if (j == 0)
                    {
                        rHtml.Append("<li><a href='#'>Prev</a></li>");
                    }
                    else if (j != count && count % 10 != 0)
                    {
                        rHtml.Append("<li><a href=" + j + ">" + j + "</a></li>");
                    }
                    else if (j != count && count % 10 == 0)
                    {
                        rHtml.Clear();
                        rHtml.Append("<li><a href=" + j + ">" + j + "</a></li>");
                    }
                    else { rHtml.Append("<li><a href=" + j + 1 + ">Next</a></li>"); }
                }
                oHtml = oHtml.Replace("{$页码$}", rHtml.ToString());
                rHtml.Clear();
            }
            return oHtml;
        }
        catch (Exception ex)
        {
            HttpContext.Current.Response.Write(ex.Message);
            HttpContext.Current.Response.End();
            return null;
        }
    }


    public static bool Replace(DataTable tb, string oHtml, string expression,string filename)
    {
        try
        {
            //string filename = null;
            string mainFileName = null;
            string serverPath = HttpContext.Current.Server.MapPath("/UI/");
            string[] exp = expression.Split('|');
            string rHtml = null,_oHtml= oHtml;
            int i = 0;
            foreach (DataRow dr in tb.Rows)
            {

                //filename = DateTime.Now.ToString("yyyyMMddHHmmss_ffff", DateTimeFormatInfo.InvariantInfo);
                mainFileName = serverPath + filename;
                foreach (string e in exp)
                {
                    switch (e)
                    {
                        case "标题":
                            rHtml = dr[i].ToString();
                            oHtml = oHtml.Replace("{$" + e + "$}", rHtml.ToString());
                            break;
                        case "内容":
                            rHtml = dr[i].ToString();
                            oHtml = oHtml.Replace("{$" + e + "$}", rHtml.ToString());
                            break;
                        default:
                            break;    
                    }
                    i++;
                }
                if (oHtml != null)
                {
                    HtmlHelper.writeHtml(mainFileName + ".html", oHtml);
                }
                i = 0;
                oHtml = _oHtml;
                rHtml = null;
            }
            return true;
        }
        catch (Exception ex)
        {
            HttpContext.Current.Response.Write(ex.Message);
            HttpContext.Current.Response.End();
            return false;
        }
    }
    //replace重载
    public static bool Replace(DataTable tb, string oHtml, string expression)
    {
        try
        {
            string filename = null;
            string mainFileName = null;
            string serverPath = HttpContext.Current.Server.MapPath("/UI/");
            string[] exp = expression.Split('|');
            string rHtml = null, _oHtml = oHtml;
            int i = 0;
            foreach (DataRow dr in tb.Rows)
            {

                filename = DateTime.Now.ToString("yyyyMMddHHmmss_ffff", DateTimeFormatInfo.InvariantInfo);
                mainFileName = serverPath + filename;
                foreach (string e in exp)
                {
                    switch (e)
                    {
                        case "标题":
                            rHtml = dr[i].ToString();
                            oHtml = oHtml.Replace("{$" + e + "$}", rHtml.ToString());
                            break;
                        case "内容":
                            rHtml = dr[i].ToString();
                            oHtml = oHtml.Replace("{$" + e + "$}", rHtml.ToString());
                            break;
                        default:
                            break;
                    }
                    i++;
                }
                if (oHtml != null)
                {
                    HtmlHelper.writeHtml(mainFileName + ".html", oHtml);
                }
                i = 0;
                oHtml = _oHtml;
                rHtml = null;
            }
            return true;
        }
        catch (Exception ex)
        {
            HttpContext.Current.Response.Write(ex.Message);
            HttpContext.Current.Response.End();
            return false;
        }
    }
}